/*
 * FB driver for the ST7789 LCD Controller
 *
 * Copyright (C) 2015 Dennis Menschel
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "fbtft.h"
#include "asm-generic/gpio.h"

#define DRVNAME "fb_st7789"

#define DEFAULT_GAMMA \
	"D0 17 19 04 03 04 32 41 43 09 14 12 33 2C\n" \
	"D0 18 17 04 03 04 31 46 43 09 14 13 31 2D"

#define __RESET__	1
//#define __SPICS__	1
#define __BKLEN__	1

#define __ROT0C__	1

static int request_gpios(struct fbtft_par *par);

/**
 * init_display() - initialize the display controller
 *
 * @par: FBTFT parameter object
 *
 * Most of the commands in this init function set their parameters to the
 * same default values which are already in place after the display has been
 * powered up. (The main exception to this rule is the pixel format which
 * would default to 18 instead of 16 bit per pixel.)
 * Nonetheless, this sequence can be used as a template for concrete
 * displays which usually need some adjustments.
 *
 * Return: 0 on success, < 0 if error occurred.
 */
static int init_display(struct fbtft_par *par)
{
	//par->fbtftops.reset(par);
	gpio_set_value(par->gpio.reset, 0);
	__MDELAY(20);
	gpio_set_value(par->gpio.reset, 1);
	__MDELAY(120);

	write_reg(par, 0x28);		// DISPON (28h): Display Off
	write_reg(par, 0x11);
	mdelay(120);
	write_reg(par, 0x36, 0x00);
	write_reg(par, 0x3A, 0x05);

	write_reg(par, 0xB2, 0x0C, 0x0C, 0x00, 0x33, 0x33);

	write_reg(par, 0xB7, 0x35);
	write_reg(par, 0xBB, 0x19);

	write_reg(par, 0xC0, 0x2C);
	write_reg(par, 0xC2, 0x01, 0xFF);
	write_reg(par, 0xC3, 0x12);
	write_reg(par, 0xC4, 0x20);
	//write_reg(par, 0xC5, 0x1F);
	write_reg(par, 0xC6, 0x0F);
	write_reg(par, 0xD0, 0xA4, 0xA1);

	write_reg(par, 0xE0, 0xD0, 0x04, 0x0D, 0x11, 0x13, 0x2B, 0x3F, 0x54, 0x4C, 0x18, 0x0D, 0x0B, 0x1F, 0x23);
	write_reg(par, 0xE1, 0xD0, 0x04, 0x0C, 0x11, 0x13, 0x2C, 0x3F, 0x44, 0x51, 0x2F, 0x1F, 0x1F, 0x20, 0x23);

  	write_reg(par, 0x21);		//? INVON (21h): Display Inversion On, INVOFF (20h): Display Inversion Off
	write_reg(par, 0x28);		// DISPON (28h): Display Off

	{
			/* set windows 0 */
		unsigned short xEnd = 239;
		unsigned short yEnd = 319;
		write_reg(par, 0x36, 0x00);	//BGR==1,MY==0,MX==0,MV==0
		write_reg(par, 0x2A, 0x0, 0x0, ((xEnd >> 8) & 0xff), (xEnd & 0xff));
		write_reg(par, 0x2B, 0x0, 0x0, ((yEnd >> 8) & 0xff), (yEnd & 0xff));
		//write_reg(par, 0x2C);
	}

	return 0;
}

void do_logo_disp_color(struct fbtft_par *par, unsigned short color) {
	int __w = 240, __h = 320;
	int i, m;

	write_reg(par, 0x2C);
	for(i=0; i<__w; i++) {
		for(m=0; m<__h; m++) {
			//__MDELAY(120);
			//write_dat(par, (color>>8) & 0xff);
			write_dat(par, ((color>>8) & 0xff));
			//__MDELAY(120);
			//write_dat(par, (color>>0) & 0xff);
			write_dat(par, ((color>>0) & 0xff));
		}
	}
}


#define WRITE_BATCH_SIZE 32 
extern unsigned char res_rgb565_240x320_map[];
void do_logo_disp_datas(void *_par ) {
    struct fbtft_par *par = (struct fbtft_par *)_par;
    int total_pixels = 240 * 320;
    int total_bytes = total_pixels * 2;
    int i = 0;

    write_reg(par, 0x2C);
    for (i = 0; i < total_bytes; i += WRITE_BATCH_SIZE) {
        int current_batch_size = (total_bytes - i < WRITE_BATCH_SIZE)? total_bytes - i : WRITE_BATCH_SIZE;
        write_datas(par, &res_rgb565_240x320_map[i], current_batch_size);
    }
	__MDELAY(10);
		write_reg(par, 0x29);		// DISPON (29h): Display On
	enable_backlight();
}

void enable_backlight(void);

void do_logo_disp_data(void *_par, unsigned short *color) {
	extern unsigned char res_rgb565_240x320_map[];
	struct fbtft_par *par = (struct fbtft_par *)_par;
	int i=0; unsigned short _color = 0;
	unsigned short *_p16buf = (unsigned short *)&res_rgb565_240x320_map;
	(void)color;

	write_reg(par, 0x2C);
	for(i=0; i<(240*320); i++) {
		_color = _p16buf[i];
		write_dat(par, ((_color>>8) & 0xff));
		write_dat(par, ((_color>>0) & 0xff));
	}
	__MDELAY(10);
	enable_backlight();
}

static struct fbtft_display display = {
	.buswidth = 8,
	.bpp = 0,
	.fps = 30,
	.txbuflen = 0,
	.debug = 0, 	//0x7 | (1<<21),
	.regwidth = 8,
	.width = 240,
	.height = 320,
	.gamma_num = 2,
	.gamma_len = 14,
	.gamma = DEFAULT_GAMMA,
	.fbtftops = {
		.init_display = init_display,
		.request_gpios = request_gpios,
	},
};

int fbtft_display_init(void)
{
	extern int fbtft_probe_common(struct fbtft_display *display);
	printf("st7789 init\n");
	int ret = fbtft_probe_common(&display);
	return ret;
}

#define REG_TOP_PIN_BASE    0x10200000

#define REG_SPI_CSN_PIN     (0x064)		//GPIO2_7
#define REG_SPI_CLK_PIN     (0x05C)
#define REG_SPI_SDO_PIN     (0x058)
#define REG_SPI_SDA_PIN     (0x060)
#define REG_GPIO3_1_PIN     (0x054)
#define REG_GPIO3_5_PIN     (0x050)
#define REG_GPIO5_0_PIN		(0x028)

#define REG_LCD_RSX_PIN		REG_GPIO3_1_PIN
#define REG_LCD_RST_PIN		REG_GPIO3_5_PIN
#define REG_LCD_BKL_PIN		REG_GPIO5_0_PIN

#define MC_GPIO3_1	(3*8+1)	//used for LCD_RSX
#define MC_GPIO3_5	(3*8+5)	//used for LCD_RST
#define MC_GPIO5_0	(5*8+0)	//used for LCD_BKL

static void __pin_init(void)
{

	volatile unsigned int reg = REG_TOP_PIN_BASE;
#ifdef __SPICS__
    writel(2, reg + REG_SPI_CSN_PIN);
#endif
    writel(2, reg + REG_SPI_CLK_PIN);
    writel(2, reg + REG_SPI_SDA_PIN);
	writel(2, reg + REG_SPI_SDO_PIN);
    writel(1, reg + REG_LCD_RSX_PIN);
#ifdef __RESET__
	writel(1, reg + REG_LCD_RST_PIN);
#endif
#ifdef __BKLEN__
	writel(1, reg + REG_LCD_BKL_PIN);
#endif

	reg = (volatile unsigned int )0x09200200;
	/* 100MHz */
	unsigned int val = readl(reg + 0x50);
	if(val != 2) {
		printf("%s 0x09200250:0x%x\n", __func__, val);
		writel(2, reg + 0x50);
	}
	val = readl(reg + 0x6c);
	if(val != 0x102) {
		printf("%s 0x0920026c:0x%x\n", __func__, val);
		//writel(0x102, reg + 0x6c);
	}
}

static int request_gpios(struct fbtft_par *par)
{
	par->gpio.dc = MC_GPIO3_1;
#ifdef __RESET__
	par->gpio.reset = MC_GPIO3_5;
#endif

#ifdef __BKLEN__
	int gpio_bkl = MC_GPIO5_0;
#endif

	__pin_init();

	if(gpio_request(par->gpio.dc, "dc-gpio"))
		printf("spi tft failed to request the gpio:%d\n", par->gpio.dc);
	gpio_direction_output(par->gpio.dc, 0);

#ifdef __BKLEN__
	/* set backlight gpio */
	if(gpio_request(gpio_bkl, "bkl-gpio"))
		printf("spi tft failed to request the bkl gpio:%d\n", gpio_bkl);
	gpio_direction_output(gpio_bkl, 1);
	gpio_set_value(gpio_bkl, 0);
#endif

#ifdef __RESET__
	if(gpio_request(par->gpio.reset, "reset-gpio"))
		printf("spi tft failed to request the gpio:%d\n", par->gpio.reset);
	gpio_direction_output(par->gpio.reset, 0);
	gpio_set_value(par->gpio.reset, 0);
#endif

	return 0;
}

void enable_backlight(void)
{
	#ifdef __BKLEN__
	gpio_set_value(MC_GPIO5_0, 1);
	#endif
}

